# 维修价格控制器
from flask import Blueprint, request, jsonify
from service.repair_price import RepairPriceService
from common.response import APIResponse

res = APIResponse()

# 创建蓝图
repair_price_bp = Blueprint('repair_price', __name__, url_prefix='/repairprice')

@repair_price_bp.route('/list', methods=['GET'])
def get_prices():
    # 获取单个价格、所有价格或按项目名称筛选
    price_id = request.args.get('id')
    item_name = request.args.get('item_name')
    
    if price_id:
        return RepairPriceService.get_price_by_id(price_id)
    elif item_name:
        return RepairPriceService.get_price_by_item_name(item_name)
    else:
        return RepairPriceService.get_all_prices(request.args)

@repair_price_bp.route('/add', methods=['POST'])
def create_price():
    # 创建新价格记录
    data = request.get_json() or {}
    if not data:
        return res.validate_error('请提供价格信息')
    return RepairPriceService.create_price(data)

@repair_price_bp.route('/update', methods=['POST'])
def update_price():
    # 更新价格记录
    data = request.get_json() or {}
    if not data or 'id' not in data:
        return res.validate_error('请提供价格ID')
    price_id = data.pop('id')
    return RepairPriceService.update_price(price_id, data)

@repair_price_bp.route('/delete', methods=['POST'])
def delete_price():
    # 删除价格记录
    data = request.get_json() or {}
    if not data or 'id' not in data:
        return res.validate_error('请提供价格ID')
    price_id = data['id']
    return RepairPriceService.delete_price(price_id)